package com.jp;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;

public class RangeSelectionView2DrawHelper {
    public static void drawBgLine(final Context context, final Canvas canvas, final Paint paint, final IntervalSelector.My my) {
        paint.reset();
        paint.setAntiAlias(true);
        paint.setColor(my.lineBg);
        //noinspection SuspiciousNameCombination
        paint.setStrokeWidth(my.lineHeight);
        canvas.drawLine(my.leftSliderEdgeCenterX, my.centerY, my.rightSliderEdgeCenterX, my.centerY, paint);
    }

    public static void drawLine(final Context context, final Canvas canvas, final Paint paint, final IntervalSelector.My my) {
        paint.reset();
        paint.setAntiAlias(true);
        paint.setColor(my.lineFg);
        //noinspection SuspiciousNameCombination
        paint.setStrokeWidth(my.lineHeight);
        canvas.drawLine(my.mDestRect.centerX(), my.centerY, my.mDestRectRight.centerX(), my.centerY, paint);

    }

    public static void drawSlider(Canvas canvas, Paint paint, IntervalSelector.My my) {
        paint.reset();
        paint.setAntiAlias(true);
        paint.setColor(Color.WHITE);
        canvas.drawBitmap(my.mSrcBitmap, my.mSrcRect, my.mDestRect, paint);
        canvas.drawBitmap(my.mSrcBitmapRight, my.mSrcRectRight, my.mDestRectRight, paint);
        if (BuildConfig.DEBUG) {
            paint.setColor(Color.GREEN);
            paint.setStyle(Paint.Style.STROKE);
            canvas.drawRect(my.mDestRect, paint);
            paint.setColor(Color.YELLOW);
            canvas.drawRect(my.mDestRectRight, paint);
        }
    }

    public static void drawSliderText(Context context, Canvas canvas, Paint paint, IntervalSelector.My my, Rect tempRect) {
        paint.reset();
        paint.setAntiAlias(true);
        paint.setColor(my.textColor);
//        paint.setColor(Color.WHITE);
        paint.setTextAlign(Paint.Align.CENTER);
        paint.setTextSize(my.textSize);

        final String leftText = my.getValueText(true);
        final String rightText = my.getValueText(false);
        paint.getTextBounds(leftText, 0, leftText.length(), tempRect);
        final float textHeight = tempRect.height();
        final float leftTextEdge = my.mDestRect.centerX() + tempRect.width() / 2f;
        paint.getTextBounds(rightText, 0, rightText.length(), tempRect);
        final float rightTextEdge = my.mDestRectRight.centerX() - tempRect.width() / 2f;
        final float maxDrawableHeight = Math.max(my.mDestRect.height(), my.mDestRectRight.height());
        final float y = getBaseLine(paint, my.centerY - maxDrawableHeight / 2f - textHeight / 2f - dp2px(context, 4));
        if (leftTextEdge >= rightTextEdge) {
            canvas.drawText(leftText + "-" + rightText, (my.mDestRect.centerX() + my.mDestRectRight.centerX()) / 2f, y, paint);
        } else {
            canvas.drawText(leftText, my.mDestRect.centerX(), y, paint);
            canvas.drawText(rightText, my.mDestRectRight.centerX(), y, paint);
        }
    }

    private static float getBaseLine(Paint paint, float textCenterY) {
        Paint.FontMetrics fontMetrics = paint.getFontMetrics();
        return textCenterY + (fontMetrics.descent - fontMetrics.ascent) / 2f - fontMetrics.descent;
    }


    private static int dp2px(final Context context, final float dp) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }
}
